import { http, type ResultBoolean, type ResultList } from "@/utils/http";

// 数据模型定义
export interface User {
  id: number;
  openid: string;
  sessionKey: string;
  nickname: string;
  avatarUrl: string;
  phoneNumber: string;
  gender: string;
  country: string;
  city: string;
  passWord: string;
  createdAt: string;
  updatedAt: string;
}

export interface UserDto {
  queryId?: string;
  pageNum?: number;
  pageSize?: number;
  queryStartTime?: string;
  queryEndTime?: string;
  orderByColumn?: string;
  isAsc?: string;
  nickname?: string;
  phoneNumber?: string;
}

export interface LoginDto {
  phoneNumber?: string;
  nickName?: string;
  passWord?: string;
}

export interface ResultUser {
  code: number;
  message: string;
  data: User;
  success: boolean;
}

export interface ResultIPageUser {
  code: number;
  message: string;
  data: {
    records: User[];
    total: number;
    size: number;
    current: number;
    pages: number;
  };
  success: boolean;
}

export interface ResultVoid {
  code: number;
  message: string;
  data: null;
  success: boolean;
}

// 原有认证相关接口
export interface UserResult {
  /**
   * 微信头像URL
   */
  avatarUrl?: string;
  city?: string;
  country?: string;
  /**
   * 创建时间
   */
  createdAt?: string;
  gender?: string;
  /**
   * 用户主键ID
   */
  id?: number;
  nickname?: string;
  openid?: string;
  /**
   * 手机号
   */
  phoneNumber?: string;
  /**
   * 微信用户 sessionKey
   */
  sessionKey?: string;
  /**
   * 登录凭证 Token
   */
  token?: string;
  /**
   * 更新时间
   */
  updatedAt?: string;
  [property: string]: any;
}

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", "/api/wx/login-by-nickname", {
    data
  });
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refresh-token", { data });
};

// 新增用户管理接口
/** 新增用户 (后台管理) */
export const createUser = (data?: User) => {
  return http.request<ResultUser>("post", "/api/user/user", { data });
};

/** 更新用户信息 */
export const updateUser = (data?: User) => {
  return http.request<ResultUser>("put", "/api/user/user", { data });
};

/** 根据ID删除用户 (后台管理) */
export const deleteUser = (id: number) => {
  return http.request<ResultVoid>("delete", `/api/user/user/${id}`);
};

/** 批量删除用户 (后台管理) */
export const batchDeleteUser = (data?: number[]) => {
  return http.request<ResultVoid>("delete", "/api/user/user/batch", { data });
};

/** 修改用户密码 (后台管理) */
export const changePassword = (data?: LoginDto) => {
  return http.request<ResultVoid>("post", "/api/user/user/change-password", {
    data
  });
};

/** 分页查询用户列表 (后台管理) */
export const getUserPage = (data?: UserDto) => {
  return http.request<ResultIPageUser>("post", "/api/user/user/page", { data });
};
export const getUserInfo = () => {
  return http.request<ResultBoolean>("get", `/system/user/info`);
};
